function loadCart() {
    // 获取uid
    let uid = localStorage.getItem('uid');
    localStorage.getItem(uid)
    // 获取商品
    let url = 'http://jx.xuzhixiang.top/ap/api/cart-list.php';
    axios.get(url, {
        params: {
            id: uid
        }
    }).then(res => {
        let arr = res.data.data;
        let html = '';
        for (let i = 0; i < arr.length; i++) {
            html += `<div class="pronews">
            <input type="checkbox" class="procheck">
            <img src="${arr[i].pimg}" alt="" class="">
            <a href="item.html?id=${arr[i].pid}" class="pname">${arr[i].pname}</a>
            <span class="shop">${arr[i].pdesc}</span>
            <p class="unitprice">${arr[i].pprice}</p>
            <div class="cartamount">
                <input type="button" value="-" class="cartminus">
                <input type="text" value="${arr[i].pnum}" class="cartqua">
                <input type="button" value="+" class="cartplus">
            </div>
            <p class="totalprice">${arr[i].pprice * arr[i].pnum}</p>       
            <input type="button" value="删除" data-id="${arr[i].pid}" class="deltbtn">    
        </div>`
        }
        let odiv = document.querySelector(".box")
        odiv.innerHTML = html;


        let Minus = document.querySelectorAll(".cartminus");
        let qua = document.querySelectorAll(".cartqua");
        let Plus = document.querySelectorAll(".cartplus");
        let Totalprice = document.querySelectorAll(".totalprice");
        let Sigprice =document.querySelectorAll(".unitprice")

        function updataData(i){
            Totalprice[i].innerText = qua[i].value*Sigprice[i].innerText;
            let obtn = document.querySelectorAll(".deltbtn");
            let pid = obtn[i].getAttribute("data-id");
            let url1 = 'http://jx.xuzhixiang.top/ap/api/cart-update-num.php';
            axios.get(url1,{
                params:{
                    uid:uid,
                    pid:pid,
                    pnum:qua[i].value
                }
            }).then(res=>{
                
            })
        }

        
        //减
        for(let i =0;i<Minus.length;i++){
            Minus[i].onclick = function(){
                qua[i].value--;
                if(qua[i].value <= 0){
                    qua[i].value = 1;
                }
                updataData(i);
                gettotalprice();
            }
           
        }

        // 加
        for(let i = 0;i<Plus.length;i++){
            Plus[i].onclick = function(){
                qua[i].value++;
                updataData(i);
                gettotalprice();
        }

        
                

        // 选中
        let obtn = document.querySelectorAll(".deltbtn");
        let acheck = document.querySelector(".overcheck");
        let sigcheck = document.querySelectorAll(".procheck")
        acheck.onclick = function () {
            for (let i = 0; i < sigcheck.length; i++) {
                sigcheck[i].checked = acheck.checked;
                
            }
            gettotalprice();
        }

        // 全选
        for (let i = 0; i < sigcheck.length; i++) {
            
            sigcheck[i].onclick = function () {
                let count = 0;
                for(let j = 0; j<sigcheck.length;j++){
                    if (sigcheck[j].checked) {
                        count++
                    }
                }

                if (count == sigcheck.length) {
                    acheck.checked = true;
                }else{
                    acheck.checked = false;
                }
                gettotalprice();
            }
            
          
        }
        // gettotalprice();


        // 删除商品

            for (let i = 0; i < obtn.length; i++) {
                obtn[i].onclick = function () {
                    let pid = obtn[i].getAttribute("data-id");
                    let obox = document.querySelectorAll(".pronews");
                    let url = 'http://jx.xuzhixiang.top/ap/api/cart-delete.php';
                    console.log(obtn[i].parentNode);
                    axios.get(url, {
                        params: {
                            uid: uid,
                            pid: pid
                        }
                    }).then(res => {
                        odiv.removeChild(obox[i]);
                        gettotalprice();
                    })
                }
                
            }

        

        function gettotalprice(){
            let ototal = document.querySelector("#total");
            let o = 0;  
            let sigcheck = document.querySelectorAll(".procheck")              
            for(let i = 0; i < sigcheck.length;i++){
                        if(sigcheck[i].checked){
                           o +=  +Totalprice[i].innerText;
                        }
                        
                    }
                    ototal.innerText = o;
                   
                    

                
            }

            
            
        }
        


    }).catch(function(err){
        
    })
}
loadCart();



